package com.ic.service.impl;

import com.ic.base.AbstractBaseService;
import com.ic.base.PageInfo;
import com.ic.dao.ItemInfoDao;
import com.ic.dao.ItemUseDao;
import com.ic.entity.ItemInfo;
import com.ic.entity.ItemUse;
import com.ic.service.ItemInfoService;
import com.ic.util.DateUtil;
import com.ic.util.KeyGenerateUtil;
import com.ic.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 入库物品明细业务类
 * Author: spark
 * Date  : 2017/6/25
 * Time  : 17:27.
 */
@Service("itemInfoService")
public class ItemInfoServiceImpl extends AbstractBaseService<ItemInfo> implements ItemInfoService {
	@Resource
	private ItemInfoDao itemInfoDao;

	@Resource
	private ItemUseDao itemUseDao;

	//物品报废
	@Value("${state.scrap}")
	private String STATE_SCRAP;

	//物品出库
	@Value("${state.outcoming}")
	private String STATE_OUTCOMING;

	//物品入库
	@Value("${state.incoming}")
	private String STATE_INCOMING;


	@Override
	public Integer insert(ItemInfo itemInfo) {
		itemInfo.setId(KeyGenerateUtil.getInstance().getKey());
		return itemInfoDao.insert(itemInfo);
	}

	@Override
	public Integer updateByPrimaryKeySelective(ItemInfo itemInfo) {
		return itemInfoDao.updateByPrimaryKeySelective(itemInfo);
	}

	/**
	 * 根据查询条件获取入库物品明细信息
	 *
	 * @param itemInfo 入库物品明细信息
	 * @param pageInfo 分页信息
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	public Map showItemInfoList(ItemInfo itemInfo, PageInfo pageInfo) {
		Map queryMap = new HashMap();
		Map resultMap = new HashMap();
		List list = new ArrayList();
		queryMap.put("subjectName", itemInfo.getSubjectName());
		queryMap.put("itemName", itemInfo.getItemName());
		queryMap.put("itemInfoNum", itemInfo.getItemInfoNum());
		queryMap.put("state", itemInfo.getState());
		queryMap.put("stateValid", STATE_SCRAP);
		// 查询符合条件的记录总数
		int totalSize = itemInfoDao.selectCount(queryMap);
		if (totalSize == 0) {
			resultMap.put("rows", 0);
			resultMap.put("total", list);
			return resultMap;
		}
		pageInfo.setTotalSize(totalSize);
		queryMap.put("startIndex", pageInfo.getStartIndex());
		queryMap.put("pageSize", pageInfo.getPAGE_SIZE());
		// 查询符合条件的记录信息
		list = itemInfoDao.showItemInfoList(queryMap);
		resultMap.put("rows", list);
		resultMap.put("total", totalSize);
		return resultMap;
	}

	/**
	 * 根据物品信息ID查询物品信息，如果被领用刚查询最新的领用信息
	 *
	 * @param id 物品信息ID
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	public Map showItemInfo(String id) {
		Map map = new HashMap<>();
		ItemInfo itemInfo = (ItemInfo) itemInfoDao.showItemInfo(id).get(0);
		map.put("itemInfo", itemInfo);
		return map;
	}

	/**
	 * 根据入库物品明细ID更改入库物品明细状态
	 *
	 * @param ids 入库物品明细ID
	 */
	@SuppressWarnings("unchecked")
	public void updateState(String[] ids, String state) {
		Map map = new HashMap();
		List idList = StringUtil.generateList(ids);
		map.put("list", idList);
		map.put("state", state);
		itemInfoDao.updateState(map);
	}

	/**
	 * 检查入库物品明细的编号是否已存在
	 *
	 * @param itemInfo 入库物品明细信息
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	public Map checkItemInfoNum(ItemInfo itemInfo) {
		Map queryMap = new HashMap();
		int count = itemInfoDao.checkItemInfoNum(itemInfo);
		queryMap.put("count", count);
		return queryMap;
	}

	/**
	 * 新增出库信息
	 *
	 * @param itemUse 出库信息
	 */
	public void addItemUseInfo(ItemUse itemUse) {
		ItemInfo itemInfo = new ItemInfo();
		itemInfo.setId(itemUse.getId());
		itemInfo.setState(STATE_OUTCOMING);
		updateByPrimaryKeySelective(itemInfo);

		itemUse.setId(KeyGenerateUtil.getInstance().getKey());
		itemUse.setItemInfoId(itemInfo.getId());
		itemUse.setAddTime(DateUtil.getNowTimeStamp());
		itemUseDao.insertSelective(itemUse);
	}

	/**
	 * 查看出库物品信息
	 *
	 * @param itemInfo 出库物品信息
	 * @return 返回Map包含itemUse信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map showItemUse(ItemInfo itemInfo) {
		Map map = new HashMap<>();
		Map queryMap = new HashMap<>();
		queryMap.put("itemInfoId", itemInfo.getId());
		queryMap.put("state", STATE_OUTCOMING);

		ItemUse itemUse = (ItemUse) itemUseDao.showItemUse(queryMap).get(0);
		String getTime = itemUse.getGetTime();
		itemUse.setGetTime(getTime.substring(0, getTime.lastIndexOf(":")));
		map.put("itemUse", itemUse);
		return map;
	}

	/**
	 * 物品入库物品
	 *
	 * @param itemUse 物品领用信息
	 */
	public void updateItemUse(ItemUse itemUse) {
		ItemInfo itemInfo = new ItemInfo();
		itemInfo.setId(itemUse.getItemInfoId());
		itemInfo.setState(STATE_INCOMING);
		itemInfoDao.updateByPrimaryKeySelective(itemInfo);

		itemUseDao.updateByPrimaryKeySelective(itemUse);
	}

	/**
	 * 根据物品ID查询物品明细及领用信息
	 *
	 * @param itemInfoId 物品ID
	 * @return 返回Map
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map showItemInfoWithItemUse(String itemInfoId) {
		Map map = new HashMap<>();
		Map queryMap = new HashMap<>();
		queryMap.put("itemInfoId", itemInfoId);
		List list = itemUseDao.showItemUse(queryMap);
		if (list != null && list.size() > 0) {
			ItemUse itemUse = (ItemUse) list.get(0);
			String getTime = itemUse.getGetTime();
			String returnTime = itemUse.getReturnTime();
			itemUse.setGetTime(StringUtil.subStringFromLastTag(getTime, ":"));
			itemUse.setReturnTime(StringUtil.subStringFromLastTag(returnTime, ":"));
			map.put("itemUse", itemUse);
			map.put("count", 1);
			return map;
		}
		ItemInfo itemInfo = (ItemInfo) itemInfoDao.showItemInfo(itemInfoId).get(0);
		ItemUse itemUse = new ItemUse();
		itemUse.setSubjectName(itemInfo.getSubjectName());
		itemUse.setItemName(itemInfo.getItemName());
		itemUse.setPrice(itemInfo.getPrice());
		itemUse.setItemInfoNum(itemInfo.getItemInfoNum());
		itemUse.setState(itemInfo.getState());
		itemUse.setRemark(itemInfo.getRemark());
		map.put("itemUse", itemUse);
		map.put("count", 0);
		return map;
	}
}
